<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Data</title>
    <style>
        body {
            min-width: 100vw;
            padding-top: 5px;
            font-family: Roboto, monospace;
        }

        .outer {
            padding: 20px;
        }

        h2, h3 {
            font-weight: normal;
        }

        hr {
            margin: 5px 0;
            clear: both;
        }

        .stats {
            float: left;
        }

    </style>
</head>
<body>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div class="outer">
    <div>
        <h1>%%URL%%</h1>
    </div>
    <div class="stats">
        <h2>Output:</h2>
        %%OUTPUT%%
    </div>
    <hr>
    <div id="chart_div"></div>
    <hr>
    <div id="chart_div_2"></div>
    <script>
        google.charts.load('current', { packages: ['corechart', 'bar', 'line'] });
        google.charts.setOnLoadCallback(draw);

        function extractTimes() {
            var maxtime = times.reduce(function(max, val) {
                return max > val ? max : val
            }, 0);
            var cnt = 1000;
            if (maxtime < cnt) {
                cnt = maxtime;
            }
            var chunk = maxtime / cnt;

            var list = [];
            for (var i = 0; i < cnt; i++) {
                list.push(null);
            }
            times.forEach(function(time) {
                var idx = (time / chunk ) | 0;
                if (!list[idx])list[idx] = [(idx * chunk | 0), 0];
                list[idx][1]++;
            });
            return list;
        }

        function drawTimes() {
            var data = new google.visualization.DataTable();
            data.addColumn('number', 'Ms');
            data.addColumn('number', 'Requests');

            data.addRows(extractTimes());

            var options = {
                chart: {
                    title: 'Average response time',
                    subtitle: ''
                },
                height: 500,
                hAxis: {
                    title: 'ms'
                },
                vAxis: {
                    title: 'Requests',
                    color: '#64B5F6'
                }
            };

            var chart = new google.charts.Bar(document.getElementById('chart_div'));
            chart.draw(data, google.charts.Bar.convertOptions(options));
        }

        function drawReqPerSec() {
            var data = new google.visualization.DataTable();
            data.addColumn('number', 'Seconds');
            data.addColumn('number', 'Pending Requests');
            data.addColumn('number', 'Timeouts');
            data.addColumn('number', 'Errors');
            data.addColumn('number', 'Min response time');
            data.addColumn('number', 'Mean response time');
            data.addColumn('number', 'Max response time');

            var rps = secondHistory.map(function(r, idx) {
                var hasTimeMetrict = r.cnt > 0;
                var val = [
                    idx,
                    r.active,
                    r.to,
                    r.err,

                    hasTimeMetrict ? r.min : null,

                    hasTimeMetrict ? r.time / r.cnt : null,

                    hasTimeMetrict ? r.max : null
                ];
                return val;
            });

            data.addRows(rps);

            var options = {
                chart: {
                    title: 'Request count / Response times',
                    subtitle: ''
                },
                height: 500,
                hAxis: {
                    title: 'seconds'
                },
                series: {
                    0: { axis: 'requests', color: '#64B5F6' },
                    1: { axis: 'requests', color: '#4A148C' },
                    2: { axis: 'requests', color: '#78909C' },
                    3: { axis: 'time', color: '#4CAF50' },
                    4: { axis: 'time', color: '#FFF176' },
                    5: { axis: 'time', color: '#e53935' }
                },
                axes: {
                    y: {
                        requests: { label: 'Requests' },
                        error: { side: 'left', label: 'Errors' },
                        time: { side: 'right', label: 'Milliseconds' }
                    }
                }
            };

            var chart = new google.charts.Line(document.getElementById('chart_div_2'));
            chart.draw(data, google.charts.Line.convertOptions(options));
        }

        function draw() {
            drawTimes();
            drawReqPerSec();
        }

        var secondHistory = '%%SECONDS%%';
        var times = '%%TIMES%%';


    </script>
</div>
</body>
</html>